home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / realasm1 / realasm1.doc < prev    next >
Encoding:
Text File  |  1993-08-07  |  9.1 KB  |  282 lines

  1. REALASM1.DOC
  2.  
  3. Download REALASM1.ZIP from Canada Remote Systems(CRS).
  4.  
  5. These functions are targeted towards those who wish to write
  6. a program in assembler and require floating point precision.
  7.  
  8. They were not designed to be added to a C program, since
  9. most C compilers come with <math.h> functions.
  10.  
  11. In writing assembler programs MODELs, small large, huge,
  12. ecetera, are NOT required. Someone once said, way back
  13. when we were limited to 640k "If your code is larger than
  14. 64k, there is something wrong with your programming style"
  15. This axiom stands true today - for assembler programs.
  16.  
  17. Part of the challenge of writing stand alone assembler
  18. programs is that you have to reinvent the wheel. For
  19. example, a heap manager, doubly-linked-lists, recursive
  20. graphics fill, windowing screen editor routines have been
  21. written by the author, but are certainly not easy to write.
  22.  
  23. I also often write programs in C and rarely supplement
  24. these programs with assember functions since C compilers
  25. are so smart (e.g. Borland C++ version 3.1).
  26.  
  27.  
  28. Floating point math functions using Microsoft MASM 6.00b
  29. --------------------------------------------------------
  30.  
  31. I have been writing in programs in machine code for many years,
  32. even before the IBM PC XT made its debut. It is a lot easier now
  33. that we have Microsoft MASM and Borland TASM but both these fine
  34. companies have closely guarded their floating point emulators.
  35.  
  36. Microsoft provides examples of integer math, quadword multiplication
  37. for example in their 'examples' that come with the assembler and in
  38. the MASM 6.0 manual they devote a whole chapter to floating point
  39. math but using the coprocessor, of course.
  40.  
  41. My inspiration was "Small Assembler - An 80x86 Macro Assembler
  42. Written in Small C" by James E. Hendrix, published by M&T Books.
  43. It comes with a disk, containing all the source code. His floating
  44. point math is of course written in C.
  45.  
  46. Before going any further, we must understand how the math
  47. coprocessor functions. The temporary real format is
  48. reviewed. This is the 80-bit format known as a REAL10
  49. in masm 6.0 jargon:
  50.  
  51.  
  52.     79   78      64 63          0
  53.   ------ ---------- -------------
  54.  | sign | exponent | significand |
  55.  |  1   |    15    |     64      |
  56.   ------ ---------- -------------
  57.                     ^
  58.                     explicit 1
  59.  
  60. SIGN:
  61. 0 = positive, 1 = negative
  62.  
  63. EXPONENT:
  64. This value determined where the binary-point is, however, it is
  65. biased with a value of 3FFFh so that all exponent values are positive.
  66. In order to get the true binary exponent, you must subtract 3FFFh.
  67.  
  68. There is one exception to this rule: if the REAL10 == 0.0,
  69. (+0.0 or -0.0), there is no bias.
  70.  
  71. Biasing allows two REAL10 numbers to be compared as if they were
  72. binary integers. When comparing them bitwise left to right, starting
  73. at bit-78, the first bit position that differs, orders the numbers.
  74.  
  75. SIGNIFICAND:
  76. In a REAL10 bit-63 is always a one (1). Formats REAL8 (double-precision)
  77. and REAL4 (single-precision) not discussed here have implicit 1's.
  78. Thus all REAL10 numbers, also known as 'normalised' numbers range
  79. between 1 and 2.
  80.  
  81.  
  82. PRECISION:
  83. The 80x87 temporary real range is 3.4 * 10**(-4932) to 1.2 * 10**(4932),
  84. which provides enormous precision far beyond what most people need.
  85. Intel provides other formats (double, single precision) and suggests
  86. that when using an 80x87, use of the temporary real value compomises
  87. the device. Rightly so.
  88.  
  89. This library provides 64-bit precision, 10**(-19) to 10**(19), for
  90. example: 1234567890123456789.1234567890123456789
  91.  
  92. Function FTRANGE returns TRUE if (+64 > exp > -64), FALSE otherwise.
  93.  
  94.  
  95. EXAMPLES (in hexadecimal):
  96.  
  97. +1.0   = 3fff 8000 0000 0000 0000        exponent ==  0
  98. +10.0  = 4002 A000 0000 0000 0000        exponent == +3
  99. pi     = 4000 C90F DAA2 2168 C235        exponent == +1
  100.  
  101. write out the high word of the significand of 10.0 in binary
  102.  
  103.                       1010000000000000
  104.  
  105. write in the binary point
  106.  
  107.                       1.010000000000000
  108.  
  109. but the exponent is +3, thus we must write
  110.  
  111.                       1010.000000000000
  112.  
  113. Try writing out the value of pi. Remember that the first digit
  114. after the decimal point = 0.5, the next 0.25, ecetera.
  115.  
  116. -------------------------------------------------
  117.  
  118. FUNCTIONS (see also MATH.INC)
  119.  
  120. TRUE          equ     1
  121. FALSE         equ     0
  122.  
  123. MAXEXPONENT   equ   64
  124. MINEXPONENT   equ  -64
  125.  
  126. szREAL10      equ   size REAL10  ; temporary real
  127. szREAL8       equ   size REAL8   ; double precision (double)
  128. szREAL4       equ   size REAL4   ; single precision (float)
  129.  
  130. NPB           typedef near ptr byte
  131. NPR10         typedef near ptr REAL10
  132. NPR8          typedef near ptr REAL8
  133. NPR4          typedef near ptr REAL4
  134.  
  135. ;---- MOVX.ASM ---- support routines
  136.  
  137. movx          proto near dst:NPB, src:NPB, wide:WORD
  138. clrx          proto near dst:NPB, wide:WORD
  139. addx          proto near dst:NPB, src:NPB, wide:WORD
  140. subx          proto near dst:NPB, src:NPB, wide:WORD
  141. lshx          proto near dst:NPB, wide:WORD
  142. rshx          proto near dst:NPB, wide:WORD
  143. cmpx          proto near dst:NPB, src:NPB, wide:WORD
  144. cmpxz         proto near dst:NPB, wide:WORD
  145.  
  146. ;---- MISC.ASM ----
  147.  
  148. ftzero        proto near dst:NPR10
  149.               Returns TRUE if dst == 0.0, disregarding sign
  150.  
  151. ftequal       proto near x:NPR10, y:NPR10
  152.               Returns TRUE if x == y, disregarding sign
  153.  
  154. ftrange       proto near dst:NPR10
  155.               Returns TRUE if (+64 > exp > -64)
  156.  
  157. ftnormal      proto near dst:NPR10
  158.               Normalises a REAL10
  159.  
  160. ftsign        proto near dst:NPR10
  161.               sign ^= 1;
  162.  
  163. ftcomp        proto near x:NPR10, y:NPR10
  164.               Returns: AX = 1,  if x > y
  165.                            -1,  if x < y
  166.                             0,  if x == y
  167.  
  168. ftswap        proto near dst:NPR10, src:NPR10
  169.               dst <---> src
  170.  
  171. LOAD constants
  172. load0         proto near dst:NPR10   dst = 0.0
  173. load1         proto near dst:NPR10   dst = 1.0
  174. load10        proto near dst:NPR10   dst = 10.0
  175. load10_19     proto near dst:NPR10   dst = 10**19, used in ft2st
  176. loadpi        proto near dst:NPR10   dst = pi
  177.  
  178. ftadd         proto near dst:NPR10, src:NPR10
  179.               dst += src
  180.  
  181. ftsub         proto near dst:NPR10, src:NPR10
  182.               dst -= src
  183.  
  184. ftmul         proto near dst:NPR10, src:NPR10
  185.               dst *= src
  186.               (shift and add multiplication)
  187.  
  188. ftdiv         proto near dst:NPR10, src:NPR10
  189.               dst /= src
  190.               (shift and subtract division)
  191.  
  192. itoft         proto near real:NPR10, integer:SWORD
  193.               Convert an integer into a REAL10
  194.  
  195. ftaddi        proto near real:NPR10, integer:SWORD
  196.               dst += integer
  197.  
  198. ftsubi        proto near real:NPR10, integer:SWORD
  199.               dst -= integer
  200.  
  201. ftmuli        proto near real:NPR10, integer:SWORD
  202.               dst *= integer
  203.  
  204. ftdivi        proto near real:NPR10, integer:SWORD
  205.               dst /= integer
  206.  
  207. ftmod         proto near dst:NPR10, src:NPR10
  208.               Returns dst /= src
  209.                       AX = number of subtractions
  210.  
  211. ftrecip       proto near dst:NPR10
  212.               Returns reciprocal of dst
  213.  
  214. ftpower       proto near real:NPR10, power:WORD
  215.               Returns real**power
  216.  
  217. ftfact        proto near real:NPR10, factorial:WORD
  218.               Returns real = factorial
  219.  
  220. ftsqrt        proto near real:NPR10
  221.               Returns square root of real
  222.               (Newton-Raphson approximation, 48 bit precision)
  223.  
  224. ftsin         proto near real:NPR10, radians:NPR10
  225.               Returns real = sin(radians)
  226.               (64-bit accuracy for pi/4 radians)
  227.  
  228. ftcos         proto near real:NPR10, radians:NPR10
  229.               Returns real = cos(radians)
  230.               (64-bit accuracy for pi/4 radians)
  231.  
  232. ftexp         proto near real:NPR10
  233.               Returns real = e**real
  234.  
  235. st2ft         proto near dst:NPR10, string:NPB
  236.               Converts string to a REAL10
  237.  
  238. st2int        proto near string:NPB
  239.               Converts string to an integer
  240.  
  241. ft2st         proto near src:NPR10, string:NPB
  242.               Converts a REAL10 to a string
  243.  
  244.  
  245.  
  246. ADDENDUM:
  247.  
  248. These functions are not included at present time. They have been
  249. written for an Fast Fourier Tranform (fft) with Parzen, Hanning,
  250. Hamming, Blackman and Welch windowing.
  251. The power spectrum function has yet to be written.
  252.  
  253. COMPLEX       struct
  254. real          REAL10  ?
  255. imag          REAL10  ?
  256. COMPLEX       ends
  257. szCOMPLEX     equ     size COMPLEX
  258.  
  259. NPCX          typedef near ptr COMPLEX
  260.  
  261. ;---- COMPLEX.ASM ------
  262.  
  263. complexadd    proto near c1:NPCX, c2:NPCX, res:NPCX
  264.               Returns res = c1 + c2
  265.  
  266. complexsub    proto near c1:NPCX, c2:NPCX, res:NPCX
  267.               Returns res = c1 - c2
  268.  
  269. complexmul    proto near c1:NPCX, c2:NPCX, res:NPCX
  270.               Returns res = c1 * c2
  271.  
  272. complexdiv    proto near c1:NPCX, c2:NPCX, res:NPCX
  273.               Returns res = c1 / c2
  274.  
  275.  
  276. Statistical functions, standard deviation, random number
  277. generator, ecetera are not presently included.
  278.  
  279. | alan.illeman@canrem.com  | "At school I was in a class of my own, |
  280. | CRS Toronto, Canada      |  all the other kids got promoted."     |
  281.  
  282.